﻿using AllInOne.Classes;
using AllInOne.MainPages;
using Newtonsoft.Json;
using System;
using System.Windows;
using System.Windows.Input;

namespace AllInOne
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        LoginVM vm = new LoginVM();

        bool IsVerify;
        public Login(bool isVerify = false)
        {
            InitializeComponent();
            this.DataContext = vm;
            IsVerify = isVerify;
        }

        private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                this.DragMove();
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnLogin_MouseDown(object sender, MouseButtonEventArgs e)
        {
            SubmitLogin();
        }

        private void BtnClose_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DialogResult = false;
            //this.Close();
        }

        private void PwdBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SubmitLogin();
            }
        }

        private void SubmitLogin()
        {
            try
            {
                string msg = "";
                if (ServiceWebApi.Login(out msg, new { UserName = vm.UserName, Password = vm.Password }))
                {
                    this.DialogResult = true;
                }
                else
                {
                    vm.Message = msg;
                }
            }
            catch (Exception ex)
            {
                vm.Message = "登录失败，请检查网络";
                SimpleConsole.WriteInfo("入厂制卡-信息录入-录入基础信息验证-异常" + ex.ToString(), this);
            }
        }
    }
}
